//记录编辑时调取代码编辑器
if(typeof CodeMirror!="undefined"){
    var editor = CodeMirror.fromTextArea(document.getElementById("code"), {
        mode: "htmlmixed",
        lineNumbers: true,
        lineWrapping: true,
    });
}

//默认加载列表
if ($("#rows").length > 0) {
    loadingData();
}

//创建记录
$('#create-bt').on('click', function (event) {
    var siteid = $(this).attr("data-siteid");
    $.ajax({
        type: "post",
        dataType: "json",
        url: "/"+$(this).attr("data-siteid")+"/page/create",
        data:{title:$("#title").val(),path:$("#path").val()},
        success: function (result) {
            if (result.code == 0) {
                $("#title").val("");
                $("#path").val("");
                $("#myModal").modal('hide');
                bootoast({
                    message: '生成成功',
                    type: 'success',
                    position:'right-bottom',
                    timeout:2
                });
                loadingData();
            } else {
                bootoast({
                    message: '创建失败，'+result.msg,
                    type: 'warning',
                    position:'right-bottom',
                    timeout:2
                });
            }
        },
        error: function() {
            bootoast({
                message: '接口异常',
                type: 'danger',
                position:'right-bottom',
                timeout:2
            });
        }
    });
    return false;
})

//生成html
$("body").on("click", ".generator-bt", function(event) {
  $.ajax({
    type: "post",
    dataType: "json",
    url: "/"+$(this).attr("data-siteid")+"/generator/create",
    data:{id:$(this).attr("data-id"),plugin:'page'},
    success: function (result) {
        if (result.code == 0) {
            bootoast({
                message: '生成成功',
                type: 'success',
                position:'right-bottom',
                timeout:2
            });
        } else {
            bootoast({
                message: '生成失败，'+result.msg,
                type: 'warning',
                position:'right-bottom',
                timeout:2
            });
        }
    },
    error : function() {
        bootoast({
            message: '异常',
            type: 'danger',
            position:'right-bottom',
            timeout:2
          });
    }
  });
  return false;
})

//保存更改
$(".save").bind('click', function() {
    var obj = $(this);
    $.ajax({
        type: "POST",
        dataType: "json",
        url: "/"+$(this).attr("data-siteid")+"/page/save",
        data: {content:editor.getValue(),id:$(this).attr("data-id")},
        success: function (result) {
            if (result.code == 0) {
                obj.html('<span class="glyphicon glyphicon-floppy-saved" aria-hidden="true" ></span> 保存成功');
                setTimeout(function(){
                    obj.html('<span class="glyphicon glyphicon-floppy-disk" aria-hidden="true" ></span> 保存更改');
                },2000)
            } else {
                obj.html('<span class="glyphicon glyphicon-floppy-remove" aria-hidden="true" ></span> 保存失败');
                setTimeout(function(){
                    obj.html('<span class="glyphicon glyphicon-floppy-disk" aria-hidden="true" ></span> 保存更改');
                },2000)
                bootoast({
                    message: '保存失败，'+result.msg,
                    type: 'warning',
                    position:'right-bottom',
                    timeout:2
                });
            }
        },
        error : function() {
            bootoast({
                message: '异常',
                type: 'danger',
                position:'right-bottom',
                timeout:2
            });
        }
    });
});

//读取记录列表
function loadingData(page = 1)
{
    var siteid = $("#create-bt").attr("data-siteid");
    $.ajax({
      type : "get",
      url: "/"+siteid+"/page/rows?page="+page,
      beforeSend:function(XMLHttpRequest){
        if (page == 1) {
            $("#rows").html('<div class="text-center"><img src=\"/static/common/img/loading.gif\" width="48"></div>');
        }
      },
      success: function (result) {
        if (page == 1) {
            $("#rows").empty();
        }
        if(result.current == 0 && page == 1){
            $("#rows").append('<div class="well well-lg text-center">哇偶！ 下空空如也，啥也没有。</div>');
        } else {
            if (page > 1) {
                var html = '';
                html += loopRows(siteid, result.data);
                $("#tb").append(html);
                if (result.pagesize > result.current || page == result.total) {
                    $("#more").hide();
                }
            } else {
                var html = '<table class="table table-hover"><thead><tr><th width="5%"><div class="checkbox checkbox-primary checkbox-single m-r-15"><input id="action-checkbox" title="全选" name="all" type="checkbox"><label for="action-checkbox"></label></div></th><th>标题</th><th width="10%">文件大小</th><th width="16%">修改时间</th><th width="15%">操作</th></tr></thead><tbody id="tb">';
                    html += loopRows(siteid, result.data);
                html += '</tbody></table>';
                if (result.pagesize == result.current) {
                    html += '<div class="text-center"><a href="javascript:;" id="more" data-page="2">加载更多...</a></div>';
                }
                $("#rows").append(html);
            }
        }
      },
      error : function() {
        if (page == 1) {
            $("#rows").empty();
        }
        alert("接口异常");
      }
    });
}

//删除记录
function deleteRows(ids)
{
    $.ajax({
        type: "post",
        dataType: "json",
        url: "/"+$("#create-bt").attr("data-siteid")+"/page/delete",
        data:{ids:ids},
        success: function (result) {
            if (result.code == 0) {
                for (var key in result.data) {
                    $("#tr-"+result.data[key]).remove();
                }
                bootoast({
                    message: '删除成功，'+result.msg,
                    type: 'success',
                    position:'right-bottom',
                    timeout:2
                });
            } else {
                bootoast({
                    message: '删除失败，'+result.msg,
                    type: 'warning',
                    position:'right-bottom',
                    timeout:2
                });
            }
        },
        error : function() {
            bootoast({
                message: '接口异常',
                type: 'danger',
                position:'right-bottom',
                timeout:2
            });
        }
    });
}

//循环列表
function loopRows(siteid,result)
{
    var html = '';
    for (var key in result) {
        html += '<tr id="tr-'+key+'"><th><div class="checkbox checkbox-primary m-r-15"><input name="ids" type="checkbox" value="'+key+'"><label for="checkbox2"></label></div></th><td><a target="_blank" href="/'+siteid+'/page/edit?id='+key+'">'+result[key]['title']+'</a></td><td>'+result[key]['size']+'</td><td>'+result[key]['friendlytime']+'</td><td><div class="btn-group"><a target="_blank" href="'+result[key]['url']+'" class="btn btn-default btn-sm">预览</a><a href="javascript:;" class="btn btn-default btn-sm delete-bt" data-siteid="'+siteid+'" data-id="'+key+'">删除</a><a href="javascript:;" class="btn btn-default btn-sm generator-bt" data-siteid="'+siteid+'" data-id="'+key+'">静态化</a></div></td></tr>';
    }
    return html;
}
